package com.chaoyong.higo.fragment;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Instrumentation.ActivityResult;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chaoyong.higo.R;
import com.chaoyong.higo.base.BaseFragment;
import com.chaoyong.higo.cainiaowo.CTitleBar;
import com.chaoyong.higo.cainiaowo.CartAdapter;
import com.chaoyong.higo.cainiaowo.CartProvider;
import com.chaoyong.higo.cainiaowo.DividerItemDecoration;
import com.chaoyong.higo.cainiaowo.ShoppingCart;
import com.chaoyong.higo.net.OApi;
import com.chaoyong.higo.utils.EmptyUtil;
import com.chaoyong.higo.utils.V;
import com.chaoyong.higo.view.BadgeView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class FragShoppingBar2 extends BaseFragment implements OnClickListener {

	private CTitleBar titleView;
	private RecyclerView recyclerView;
	private LinearLayout total_price_lay;
	private LinearLayout complete_lay;
	private LinearLayout delete_lay;
	private CheckBox mCheckBox;
	private TextView text_total;
	private TextView goods_counts;

	private CartAdapter mAdapter;
	private CartProvider cartProvider;
	private List<ShoppingCart> carts;
	private ImageView cfragshop_null;
	
	private BadgeView badgeView;

	public static final int ACTION_EDIT = 1;
	public static final int ACTION_CAMPLATE = 2;

	public FragShoppingBar2(BadgeView badgeView) {
		// TODO Auto-generated constructor stub
		this.badgeView = badgeView;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.frag_shopping_bar2, container, false);
		return mView;
	}

	@Override
	public void findView() {
		titleView = V.f(mView, R.id.cfragshop_titleView);
		recyclerView = V.f(mView, R.id.cfragshop_RecyclerView);
		total_price_lay = V.f(mView, R.id.total_price_lay);
		complete_lay = V.f(mView, R.id.complete_lay);
		delete_lay = V.f(mView, R.id.delete_lay);
		mCheckBox = V.f(mView, R.id.checkbox_all);
		text_total = V.f(mView, R.id.text_total);
		goods_counts = V.f(mView, R.id.goods_counts);
		cfragshop_null = V.f(mView, R.id.cfragshop_null);
	}

	@Override
	public void init() {
		cartProvider = new CartProvider(mContext);
		changeTitleBar();
//		showData();

		delete_lay.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// showToast("删除");
				mAdapter.delCart();
			}
		});

		// complete_lay.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		//// showToast("结算" + "");
		// //提交到服务器
		// }
		// });
	}
	
	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		//mAdapter.delCart();
		//getCarts();
		//int success = getActivity().getIntent().getIntExtra("success", 0);
		/*int success = getArguments().getInt("success");
		Toast.makeText(mContext, success + "", 0).show();*/
		showData();
		/*mAdapter.notifyDataSetChanged();
		mAdapter.showTotalPrice();*/
	}

	private void getCarts() {
		// TODO Auto-generated method stub
		HttpUtils utils = new HttpUtils();
		utils.send(HttpMethod.GET, OApi.Public_sess, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, arg1, 0).show();
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				// TODO Auto-generated method stub
				String result = arg0.result;
				try {
					JSONObject json = new JSONObject(result);
					
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}

	public void delAllCart(){
		mAdapter.delAllCart();
	}
	
	public void refrush(){
		mAdapter.notifyDataSetChanged();
	}
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		if (!EmptyUtil.isEmpty(carts)) {
			mAdapter.refData(carts);
		}
		
		if (OApi.isPaySuccesss == 1) {
			mAdapter.delCart();
		}
		
		/*if (EmptyUtil.isEmpty(carts.size())) {
			goods_counts.setText("0");
			cfragshop_null.setVisibility(View.VISIBLE);
		}else {
			goods_counts.setText("" + carts.size());
		}*/
	}

	private void showData() {
		carts = cartProvider.getAll();
		/*if(carts.size() == 0){
			cfragshop_null.setVisibility(View.VISIBLE);
		}else{
			cfragshop_null.setVisibility(View.GONE);
		}*/
		
		mAdapter = new CartAdapter(mContext, carts, mCheckBox, text_total, complete_lay, goods_counts,cfragshop_null,badgeView);
		recyclerView.setAdapter(mAdapter);
		recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
		recyclerView.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST));
	}

	private void changeTitleBar() {
		titleView.getRightButton().setVisibility(View.VISIBLE);
		titleView.setRightButtonText("编辑");

		titleView.getRightButton().setOnClickListener(this);

		titleView.getRightButton().setTag(ACTION_EDIT);
	}

	private void showDelControl() {
		titleView.getRightButton().setText("完成");
		total_price_lay.setVisibility(View.INVISIBLE);
		complete_lay.setVisibility(View.GONE);
		delete_lay.setVisibility(View.VISIBLE);
		titleView.getRightButton().setTag(ACTION_CAMPLATE);

		mAdapter.checkAll_None(false);

		mCheckBox.setChecked(false);

		mAdapter.setTextView(text_total);
		;
	}

	private void hideDelControl() {
		total_price_lay.setVisibility(View.VISIBLE);
		complete_lay.setVisibility(View.VISIBLE);

		delete_lay.setVisibility(View.GONE);
		titleView.setRightButtonText("编辑");
		titleView.getRightButton().setTag(ACTION_EDIT);

		mAdapter.checkAll_None(true);
		mAdapter.showTotalPrice();
		mCheckBox.setChecked(true);
	}

	@Override
	public void onClick(View v) {
		int action = (int) v.getTag();
		if (ACTION_EDIT == action) {
			showDelControl();
		} else if (ACTION_CAMPLATE == action) {
			hideDelControl();
		}
	}

	/*@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		Toast.makeText(mContext, "=========", 0).show();
		if (resultCode == 0) {  
			Toast.makeText(mContext, "resultCode = " + resultCode, 0).show();
            mAdapter.delAllCart();
        }  
         
	}*/

}
